package LoginHandler

import (
	"fmt"
	"time"

	"../ProtoGo"
	"../Robot"

	"github.com/golang/protobuf/proto"
)

var command = Cmd.Command_LOGIN_USER_PROTOCMD.Enum()

//StartLogin 发送登录协议
func StartLogin(robot *Robot.Robot) {

	param := Cmd.LoginCmdParam_REQ_LOGIN_USER_CMD.Enum()

	req := &Cmd.ReqLoginUserCmd{}
	req.Cmd = command
	req.Param = param
	req.Accid = proto.Uint64(robot.Account.Accid)

	timestamp := time.Now().Unix()
	strLoginSign := "orgdy7EucsHWUYsQdy622qm6CUgodTUqbFvC6MCg"
	s := fmt.Sprintf("%d_%d_%s", robot.Account.Accid, timestamp, strLoginSign)

	req.Sha1 = proto.String(Robot.CalculateSHA1(s))
	req.Zoneid = proto.Uint32(robot.Account.Zoneid)
	req.Timestamp = proto.Uint32(uint32(timestamp))
	robot.SendProto(byte(*command), byte(*param), req, true)
}

//RegisterLoginHandler 注册处理登录相关方法
func RegisterLoginHandler(r *Robot.Robot) {
	cc := Cmd.Command_LOGIN_USER_PROTOCMD.Enum()

	r.RobotLog("注册登录模块")
	r.Handler.SetModuleBegin(int32(*cc), StartLogin)

	pp := Cmd.LoginCmdParam_SNAPSHOT_USER_CMD.Enum()
	r.Handler.RegisterHandle(byte(*cc), byte(*pp), handleSnapShotUserCmd)

	pp = Cmd.LoginCmdParam_SAFE_DEVICE_USER_CMD.Enum()
	r.Handler.RegisterHandle(byte(*cc), byte(*pp), handleSafeDeviceUserCmd)

	pp = Cmd.LoginCmdParam_CONFIRM_AUTHORIZE_USER_CMD.Enum()
	r.Handler.RegisterHandle(byte(*cc), byte(*pp), handleConfirmAuthorizeUserCmd)

	pp = Cmd.LoginCmdParam_LOGIN_RESULT_USER_CMD.Enum()
	r.Handler.RegisterHandle(byte(*cc), byte(*pp), handleLoginResultUserCmd)

	pp = Cmd.LoginCmdParam_REAL_AUTHORIZE_USER_CMD.Enum()
	r.Handler.RegisterHandle(byte(*cc), byte(*pp), handleRealAuthorizeUserCmd)

	pp = Cmd.LoginCmdParam_SERVERTIME_USER_CMD.Enum()
	r.Handler.RegisterHandle(byte(*cc), byte(*pp), handleServerTimeUserCmd)

	pp = Cmd.LoginCmdParam_HEART_BEAT_USER_CMD.Enum()
	r.Handler.RegisterHandle(byte(*cc), byte(*pp), handleHeartBeatUserCmd)
}

//HandleSnapShotUserCmd 处理登录返回消息
func handleSnapShotUserCmd(xcmd *Robot.XCommand, robot *Robot.Robot) {
	response := &Cmd.SnapShotUserCmd{}
	if err := proto.Unmarshal(xcmd.Probuf, response); err != nil {
		robot.RobotErr("序列化SnapShotUserCmd")
	} else {
		robot.RobotLog("接收到协议SnapShotUserCmd %s", response.String())
	}
	rolenum := len(response.Data)
	if rolenum == 0 {
		robot.RobotLog("没有任何角色,开始创建角色")
		request := createCharacter(robot.Account.Accid)
		param := Cmd.LoginCmdParam_CREATE_CHAR_USER_CMD.Enum()
		robot.SendProto(byte(*command), byte(*param), request, true)
	} else {
		charid := response.Data[0].GetId()
		robot.RobotLog("拥有%d角色", rolenum)
		robot.RobotLog("选择角色:%d", charid)

		request := &Cmd.SelectRoleUserCmd{}
		request.Id = proto.Uint64(charid)
		param := Cmd.LoginCmdParam_SELECT_ROLE_USER_CMD.Enum()
		robot.SendProto(byte(*command), byte(*param), request, true)
	}
	//收到这条协议表示这个模块所有协议结束
	cc := Cmd.Command_LOGIN_USER_PROTOCMD.Enum()
	robot.Handler.FinishModule(int32(*cc))
}

func handleSafeDeviceUserCmd(xcmd *Robot.XCommand, robot *Robot.Robot) {
	response := &Cmd.SafeDeviceUserCmd{}
	if err := proto.Unmarshal(xcmd.Probuf, response); err != nil {
		robot.RobotErr("序列化SafeDeviceUserCmd")
	} else {
		robot.RobotLog("接收到协议SafeDeviceUserCmd %s", response.String())
	}
}

func handleConfirmAuthorizeUserCmd(xcmd *Robot.XCommand, robot *Robot.Robot) {
	response := &Cmd.ConfirmAuthorizeUserCmd{}
	if err := proto.Unmarshal(xcmd.Probuf, response); err != nil {
		robot.RobotErr("序列化ConfirmAuthorizeUserCmd")
	} else {
		robot.RobotLog("接收到协议ConfirmAuthorizeUserCmd %s ", response.String())
	}
}

func createCharacter(accid uint64) proto.Message {
	req := &Cmd.CreateCharUserCmd{}
	req.Name = proto.String(fmt.Sprintf("ro%d", accid))
	req.RoleSex = proto.Uint32(2)
	req.Profession = proto.Uint32(14) //骑士
	req.Hair = proto.Uint32(2)
	req.Haircolor = proto.Uint32(2)
	req.Clothcolor = proto.Uint32(1)
	req.Accid = proto.Uint64(accid)
	req.Sequence = proto.Uint32(1)
	//req.Version = proto.String()
	return req
}

func handleRealAuthorizeUserCmd(xcmd *Robot.XCommand, robot *Robot.Robot) {
	response := &Cmd.RealAuthorizeUserCmd{}
	if err := proto.Unmarshal(xcmd.Probuf, response); err != nil {
		robot.RobotErr("序列化RealAuthorizeUserCmd")
	} else {
		robot.RobotLog("接收到协议RealAuthorizeUserCmd %s", response.String())
	}
}

func handleLoginResultUserCmd(xcmd *Robot.XCommand, robot *Robot.Robot) {
	response := &Cmd.LoginResultUserCmd{}
	if err := proto.Unmarshal(xcmd.Probuf, response); err != nil {
		robot.RobotErr("序列化LoginResultUserCmd")
	} else {
		robot.RobotLog(response.String())
	}
}

func handleServerTimeUserCmd(xcmd *Robot.XCommand, robot *Robot.Robot) {
	response := &Cmd.ServerTimeUserCmd{}
	if err := proto.Unmarshal(xcmd.Probuf, response); err != nil {
		robot.RobotErr("序列化ServerTimeUserCmd")
	} else {
		robot.RobotLog(response.String())
	}
}

func handleHeartBeatUserCmd(xcmd *Robot.XCommand, robot *Robot.Robot) {
	response := &Cmd.HeartBeatUserCmd{}
	if err := proto.Unmarshal(xcmd.Probuf, response); err != nil {
		robot.RobotErr("序列化HeartBeatUserCmd")
	} else {
		robot.RobotLog("接收到协议HeartBeatUserCmd %s", response.String())
	}
	request := &Cmd.HeartBeatUserCmd{}
	param := Cmd.LoginCmdParam_HEART_BEAT_USER_CMD.Enum()
	robot.SendProto(byte(*command), byte(*param), request, false)
}
